home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / gnu / gpp-1_42.lha / g++-1.42.0 / gcc.c < prev    next >
C/C++ Source or Header  |  1991-10-19  |  55KB  |  2,160 lines

  1. /* Compiler driver program that can handle many languages.
  2.    Copyright (C) 1987,1989 Free Software Foundation, Inc.
  3.  
  4. This file is part of GNU CC.
  5.  
  6. GNU CC is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 1, or (at your option)
  9. any later version.
  10.  
  11. GNU CC is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with GNU CC; see the file COPYING.  If not, write to
  18. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  19.  
  20. This paragraph is here to try to keep Sun CC from dying.
  21. The number of chars here seems crucial!!!!  */
  22.  
  23. void record_temp_file ();
  24.  
  25. /* This program is the user interface to the C compiler and possibly to
  26. other compilers.  It is used because compilation is a complicated procedure
  27. which involves running several programs and passing temporary files between
  28. them, forwarding the users switches to those programs selectively,
  29. and deleting the temporary files at the end.
  30.  
  31. CC recognizes how to compile each input file by suffixes in the file names.
  32. Once it knows which kind of compilation to perform, the procedure for
  33. compilation is specified by a string called a "spec".
  34.  
  35. Specs are strings containing lines, each of which (if not blank)
  36. is made up of a program name, and arguments separated by spaces.
  37. The program name must be exact and start from root, since no path
  38. is searched and it is unreliable to depend on the current working directory.
  39. Redirection of input or output is not supported; the subprograms must
  40. accept filenames saying what files to read and write.
  41.  
  42. In addition, the specs can contain %-sequences to substitute variable text
  43. or for conditional text.  Here is a table of all defined %-sequences.
  44. Note that spaces are not generated automatically around the results of
  45. expanding these sequences; therefore, you can concatenate them together
  46. or with constant text in a single argument.
  47.  
  48.  %%    substitute one % into the program name or argument.
  49.  %i     substitute the name of the input file being processed.
  50.  %b     substitute the basename of the input file being processed.
  51.     This is the substring up to (and not including) the last period.
  52.  %g     substitute the temporary-file-name-base.  This is a string chosen
  53.     once per compilation.  Different temporary file names are made by
  54.     concatenation of constant strings on the end, as in `%g.s'.
  55.     %g also has the same effect of %d.
  56.  %d    marks the argument containing or following the %d as a
  57.     temporary file name, so that that file will be deleted if CC exits
  58.     successfully.  Unlike %g, this contributes no text to the argument.
  59.  %w    marks the argument containing or following the %w as the
  60.     "output file" of this compilation.  This puts the argument
  61.     into the sequence of arguments that %o will substitute later.
  62.  %o    substitutes the names of all the output files, with spaces
  63.     automatically placed around them.  You should write spaces
  64.     around the %o as well or the results are undefined.
  65.     %o is for use in the specs for running the linker.
  66.     Input files whose names have no recognized suffix are not compiled
  67.     at all, but they are included among the output files, so they will
  68.     be linked.
  69.  %r     substitutes the standard start file prefix.
  70.  %p    substitutes the standard macro predefinitions for the
  71.     current target machine.  Use this when running cpp.
  72.  %P    like %p, but puts `__' before and after the name of each macro.
  73.     This is for ANSI C.
  74.  %s     current argument is the name of a library or startup file of some sort.
  75.         Search for that file in a standard list of directories
  76.     and substitute the full pathname found.
  77.  %eSTR  Print STR as an error message.  STR is terminated by a newline.
  78.         Use this when inconsistent options are detected.
  79.  %a     process ASM_SPEC as a spec.
  80.         This allows config.h to specify part of the spec for running as.
  81.  %l     process LINK_SPEC as a spec.
  82.  %L     process LIB_SPEC as a spec.
  83.  %S     process STARTFILE_SPEC as a spec.  A capital S is actually used here.
  84.  %c    process SIGNED_CHAR_SPEC as a spec.
  85.  %C     process CPP_SPEC as a spec.  A capital C is actually used here.
  86.  %1    process CC1_SPEC as a spec.
  87.  %{S}   substitutes the -S switch, if that switch was given to CC.
  88.     If that switch was not specified, this substitutes nothing.
  89.     Here S is a metasyntactic variable.
  90.  %{S*}  substitutes all the switches specified to CC whose names start
  91.     with -S.  This is used for -o, -D, -I, etc; switches that take
  92.     arguments.  CC considers `-o foo' as being one switch whose
  93.     name starts with `o'.  %{o*} would substitute this text,
  94.     including the space; thus, two arguments would be generated.
  95.  %{S:X} substitutes X, but only if the -S switch was given to CC.
  96.  %{!S:X} substitutes X, but only if the -S switch was NOT given to CC.
  97.  %{|S:X} like %{S:X}, but if no S switch, substitute `-'.
  98.  %{|!S:X} like %{!S:X}, but if there is an S switch, substitute `-'.
  99.  
  100. The conditional text X in a %{S:X} or %{!S:X} construct may contain
  101. other nested % constructs or spaces, or even newlines.
  102. They are processed as usual, as described above.
  103.  
  104. The character | is used to indicate that a command should be piped to
  105. the following command, but only if -pipe is specified.
  106.  
  107. Note that it is built into CC which switches take arguments and which
  108. do not.  You might think it would be useful to generalize this to
  109. allow each compiler's spec to say which switches take arguments.  But
  110. this cannot be done in a consistent fashion.  CC cannot even decide
  111. which input files have been specified without knowing which switches
  112. take arguments, and it must know which input files to compile in order
  113. to tell which compilers to run.
  114.  
  115. CC also knows implicitly that arguments starting in `-l' are to
  116. be treated as compiler output files, and passed to the linker in their proper
  117. position among the other output files.
  118.  
  119. */
  120.  
  121. #include <stdio.h>
  122. #include <sys/types.h>
  123. #include <signal.h>
  124. #include <sys/file.h>
  125.  
  126. #include "config.h"
  127. #include "obstack.h"
  128. #include "gvarargs.h"
  129.  
  130. #ifdef USG
  131. #define R_OK 4
  132. #define W_OK 2
  133. #define X_OK 1
  134. #define vfork fork
  135. #endif /* USG */
  136.  
  137. #define obstack_chunk_alloc xmalloc
  138. #define obstack_chunk_free free
  139. extern int xmalloc ();
  140. extern void free ();
  141.  
  142. /* If a stage of compilation returns an exit status >= 1,
  143.    compilation of that file ceases.  */
  144.  
  145. #define MIN_FATAL_STATUS 1
  146.  
  147. /* This is the obstack which we use to allocate many strings.  */
  148.  
  149. struct obstack obstack;
  150.  
  151. char *handle_braces ();
  152. char *save_string ();
  153. char *concat ();
  154. int do_spec ();
  155. int do_spec_1 ();
  156. char *find_file ();
  157. static char *find_exec_file ();
  158. void validate_switches ();
  159. void validate_all_switches ();
  160. void fancy_abort ();
  161.  
  162. /* config.h can define ASM_SPEC to provide extra args to the assembler
  163.    or extra switch-translations.  */
  164. #ifndef ASM_SPEC
  165. #define ASM_SPEC ""
  166. #endif
  167.  
  168. /* config.h can define CPP_SPEC to provide extra args to the C preprocessor
  169.    or extra switch-translations.  */
  170. #ifndef CPP_SPEC
  171. #define CPP_SPEC ""
  172. #endif
  173.  
  174. /* config.h can define CC1_SPEC to provide extra args to cc1
  175.    or extra switch-translations.  */
  176. #ifndef CC1_SPEC
  177. #define CC1_SPEC ""
  178. #endif
  179.  
  180. /* config.h can define LINK_SPEC to provide extra args to the linker
  181.    or extra switch-translations.  */
  182. #ifndef LINK_SPEC
  183. #define LINK_SPEC ""
  184. #endif
  185.  
  186. /* config.h can define LIB_SPEC to override the default libraries.  */
  187. #ifndef LIB_SPEC
  188. #define LIB_SPEC "%{!p:%{!pg:-lc}}%{p:-lc_p}%{pg:-lc_p}"
  189. #endif
  190.  
  191. /* config.h can define STARTFILE_SPEC to override the default crt0 files.  */
  192. #ifndef STARTFILE_SPEC
  193. #define STARTFILE_SPEC  \
  194.   "%{pg:gcrt0.o%s}%{!pg:%{p:mcrt0.o%s}%{!p:crt0.o%s}}"
  195. #endif
  196.  
  197. /* This spec is used for telling cpp whether char is signed or not.  */
  198. #define SIGNED_CHAR_SPEC  \
  199.   (DEFAULT_SIGNED_CHAR ? "%{funsigned-char:-D__CHAR_UNSIGNED__}"    \
  200.    : "%{!fsigned-char:-D__CHAR_UNSIGNED__}")
  201.  
  202. /* This defines which switch letters take arguments.  */
  203.  
  204. #ifndef SWITCH_TAKES_ARG
  205. #define SWITCH_TAKES_ARG(CHAR)      \
  206.   ((CHAR) == 'D' || (CHAR) == 'U' || (CHAR) == 'o' \
  207.    || (CHAR) == 'e' || (CHAR) == 'T' || (CHAR) == 'u' \
  208.    || (CHAR) == 'I' || (CHAR) == 'Y' || (CHAR) == 'm' \
  209.    || (CHAR) == 'L' || (CHAR) == 'i')
  210. #endif
  211.  
  212. /* This defines which multi-letter switches take arguments.  */
  213.  
  214. #ifndef WORD_SWITCH_TAKES_ARG
  215. #define WORD_SWITCH_TAKES_ARG(STR) (!strcmp (STR, "Tdata"))
  216. #endif
  217.  
  218. /* This structure says how to run one compiler, and when to do so.  */
  219.  
  220. struct compiler
  221. {
  222.   char *suffix;            /* Use this compiler for input files
  223.                    whose names end in this suffix.  */
  224.   char *spec;            /* To use this compiler, pass this spec
  225.                    to do_spec.  */
  226. };
  227.  
  228. #define cplusplus_string \
  229.   "cpp -+ %{nostdinc} %{C} %{v} %{D*} %{U*} %{I*} %{M*} %{i*} \
  230.        -undef -D__GNUC__ -D__GNUG__ -D__cplusplus %p %P\
  231.        %c %{O:-D__OPTIMIZE__} %{traditional} %{pedantic}\
  232.        %{Wcomment*} %{Wtrigraphs} %{Wall} %C\
  233.        %i %{!M*:%{!E:%{!pipe:%g.cpp}}}%{E:%{o*}}%{M*:%{o*}} |\n\
  234.    %{!M*:%{!E:cc1plus %{!pipe:%g.cpp} %1\
  235.          %{!Q:-quiet} -dumpbase %i %{Y*} %{d*} %{m*} %{f*} %{+e*} %{a}\
  236.          %{g} %{g0} %{O} %{W*} %{w} %{pedantic} %{traditional}\
  237.          %{v:-version} %{gg:-symout %g.sym} %{pg:-p} %{p} %{xref}\
  238.          %{pg:%{fomit-frame-pointer:%e-pg and -fomit-frame-pointer are incompatible}}\
  239.          %{S:%{o*}%{!o*:-o %b.s}}%{!S:-o %{|!pipe:%g.s}} |\n\
  240.          %{!S:as %{R} %{j} %{J} %{h} %{d2} %a %{gg:-G %g.sym}\
  241.         %{c:%{o*}%{!o*:-o %w%b.o}}%{!c:-o %d%w%b.o}\
  242.                    %{!pipe:%g.s}\n }}}"
  243.  
  244. #define cplusplus_string1 \
  245.   "cc1plus %i %1\
  246.          %{!Q:-quiet} -dumpbase %i %{Y*} %{d*} %{m*} %{f*} %{+e*} %{a}\
  247.          %{g} %{g0} %{O} %{W*} %{w} %{pedantic} %{traditional}\
  248.          %{v:-version} %{gg:-symout %g.sym} %{pg:-p} %{p} %{xref}\
  249.          %{pg:%{fomit-frame-pointer:%e-pg and -fomit-frame-pointer are incompatible}}\
  250.          %{S:%{o*}%{!o*:-o %b.s}}%{!S:-o %{|!pipe:%g.s}} |\n\
  251.          %{!S:as %{R} %{j} %{J} %{h} %{d2} %a %{gg:-G %g.sym}\
  252.         %{c:%{o*}%{!o*:-o %w%b.o}}%{!c:-o %d%w%b.o}\
  253.                    %{!pipe:%g.s}\n }"
  254.  
  255. /* Here are the specs for compiling files with various known suffixes.
  256.    A file that does not end in any of these suffixes will be passed
  257.    unchanged to the loader and nothing else will be done to it.  */
  258.  
  259. struct compiler compilers[] =
  260. {
  261.   {".c",
  262. #if 0
  263.    "cpp %{nostdinc} %{C} %{v} %{D*} %{U*} %{I*} %{M*} %{i*} %{trigraphs} -undef \
  264.         -D__GNUC__ %{ansi:-trigraphs -$ -D__STRICT_ANSI__} %{!ansi:%p} %P\
  265.         %c %{O:-D__OPTIMIZE__} %{traditional} %{pedantic}\
  266.     %{Wcomment*} %{Wtrigraphs} %{Wall} %C\
  267.         %i %{!M*:%{!E:%{!pipe:%g.cpp}}}%{E:%{o*}}%{M*:%{o*}} |\n\
  268.     %{!M*:%{!E:cc1 %{!pipe:%g.cpp} %1 \
  269.            %{!Q:-quiet} -dumpbase %i %{Y*} %{d*} %{m*} %{f*} %{a}\
  270.            %{g} %{O} %{W*} %{w} %{pedantic} %{ansi} %{traditional}\
  271.            %{v:-version} %{gg:-symout %g.sym} %{pg:-p} %{p} %{xref}\
  272.            %{pg:%{fomit-frame-pointer:%e-pg and -fomit-frame-pointer are incompatible}}\
  273.            %{S:%{o*}%{!o*:-o %b.s}}%{!S:-o %{|!pipe:%g.s}} |\n\
  274.               %{!S:as %{R} %{j} %{J} %{h} %{d2} %a %{gg:-G %g.sym}\
  275.               %{c:%{o*}%{!o*:-o %w%b.o}}%{!c:-o %d%w%b.o}\
  276.                       %{!pipe:%g.s}\n }}}"
  277. #else
  278.    cplusplus_string
  279. #endif
  280.    },
  281.  
  282.   {".cc",
  283.    cplusplus_string},
  284.   {".C",
  285.    cplusplus_string},
  286.   {".cxx",
  287.    cplusplus_string},
  288.  
  289.   {".i",
  290.    cplusplus_string1},
  291.   {".s",
  292.    "%{!S:as %{R} %{j} %{J} %{h} %{d2} %a \
  293.             %{c:%{o*}%{!o*:-o %w%b.o}}%{!c:-o %d%w%b.o} %i\n }"},
  294.   {".S",
  295.    "cpp %{nostdinc} %{C} %{v} %{D*} %{U*} %{I*} %{i*} %{M*} %{trigraphs} \
  296.         -undef -D__GNUC__ -$ %p %P\
  297.         %c %{O:-D__OPTIMIZE__} %{traditional} %{pedantic}\
  298.     %{Wcomment*} %{Wtrigraphs} %{Wall} %C\
  299.         %i %{!M*:%{!E:%{!pipe:%g.s}}}%{E:%{o*}}%{M*:%{o*}} |\n\
  300.     %{!M*:%{!E:%{!S:as %{R} %{j} %{J} %{h} %{d2} %a \
  301.                     %{c:%{o*}%{!o*:-o %w%b.o}}%{!c:-o %d%w%b.o}\
  302.             %{!pipe:%g.s}\n }}}"},
  303.   /* Mark end of table */
  304.   {0, 0}
  305. };
  306.  
  307. #ifdef USE_COLLECT
  308. /* C++: Here is the spec for collecting global ctor and dtor
  309.    requirements.  */
  310. char *collect_spec =
  311.   "%{!c:%{!M*:%{!E:%{!S:collect -o %g.s %g.R\n\
  312. as %g.s -o %g.O\n\
  313. ld %{o*} %g.R %g.O\n\
  314. }}}}";
  315.  
  316. /* Here is the spec for running the linker, after compiling all files.  */
  317. char *link_spec = "%{!c:%{!M*:%{!E:%{!S:ld -r -o %g.R %l\
  318.  %{A} %{d} %{e*} %{N} %{n} %{r} %{s} %{S} %{T*} %{t} %{u*} %{X} %{x} %{z}\
  319.  %{y*} %{!nostdlib:%S} \
  320.  %{L*} %o %{!nostdlib:-lg++ gnulib%s %{g:-lg} %L}\n }}}}";
  321. #else
  322. /* Here is the spec for running the linker, after compiling all files.  */
  323. char *link_spec = "%{!c:%{!M*:%{!E:%{!S:ld %{o*} %l\
  324.  %{A} %{d} %{e*} %{N} %{n} %{r} %{s} %{S} %{T*} %{t} %{u*} %{X} %{x} %{z}\
  325.  %{y*} %{static:} %{!nostdlib:%S %{pg:gccrt0.o%s}%{!pg:ccrt0.o%s}} \
  326.  %{L*} %o %{!nostdlib:-L%r -lg++ gnulib%s %{g:-lg} %L}\n }}}}";
  327. #endif
  328.  
  329. /* Accumulate a command (program name and args), and run it.  */
  330.  
  331. /* Vector of pointers to arguments in the current line of specifications.  */
  332.  
  333. char **argbuf;
  334.  
  335. /* Number of elements allocated in argbuf.  */
  336.  
  337. int argbuf_length;
  338.  
  339. /* Number of elements in argbuf currently in use (containing args).  */
  340.  
  341. int argbuf_index;
  342.  
  343. /* Number of commands executed so far.  */
  344.  
  345. int execution_count;
  346.  
  347. /* Flag indicating whether we should print the command and arguments */
  348.  
  349. unsigned char vflag;
  350.  
  351. /* Name with which this program was invoked.  */
  352.  
  353. char *programname;
  354.  
  355. /* User-specified -B prefix to attach to command names,
  356.    or 0 if none specified.  */
  357.  
  358. char *user_exec_prefix = 0;
  359. int user_exec_prefix_used = 0;
  360.  
  361. /* Environment-specified prefix to attach to command names,
  362.    or 0 if none specified.  */
  363.  
  364. char *env_exec_prefix = 0;
  365.  
  366. /* Suffix to attach to directories searched for commands.  */
  367.  
  368. char *machine_suffix = 0;
  369.  
  370. /* Default prefixes to attach to command names.  */
  371.  
  372. #ifndef STANDARD_EXEC_PREFIX
  373. #define STANDARD_EXEC_PREFIX "/usr/local/lib/gcc-"
  374. #endif /* !defined STANDARD_EXEC_PREFIX */
  375.  
  376. char *standard_exec_prefix = STANDARD_EXEC_PREFIX;
  377. char *standard_exec_prefix_1 = "/usr/lib/gcc-";
  378.  
  379. #ifndef STANDARD_STARTFILE_PREFIX
  380. #define STANDARD_STARTFILE_PREFIX "/usr/local/lib/"
  381. #endif /* !defined STANDARD_STARTFILE_PREFIX */
  382.  
  383. char *standard_startfile_prefix = STANDARD_STARTFILE_PREFIX;
  384. char *standard_startfile_prefix_1 = "/lib/";
  385. char *standard_startfile_prefix_2 = "/usr/lib/";
  386.  
  387. /* Clear out the vector of arguments (after a command is executed).  */
  388.  
  389. void
  390. clear_args ()
  391. {
  392.   argbuf_index = 0;
  393. }
  394.  
  395. /* Add one argument to the vector at the end.
  396.    This is done when a space is seen or at the end of the line.
  397.    If DELETE_ALWAYS is nonzero, the arg is a filename
  398.     and the file should be deleted eventually.
  399.    If DELETE_FAILURE is nonzero, the arg is a filename
  400.     and the file should be deleted if this compilation fails.  */
  401.  
  402. void
  403. store_arg (arg, delete_always, delete_failure)
  404.      char *arg;
  405.      int delete_always, delete_failure;
  406. {
  407.   if (argbuf_index + 1 == argbuf_length)
  408.     {
  409.       argbuf = (char **) realloc (argbuf, (argbuf_length *= 2) * sizeof (char *));
  410.     }
  411.  
  412.   argbuf[argbuf_index++] = arg;
  413.   argbuf[argbuf_index] = 0;
  414.  
  415.   if (delete_always || delete_failure)
  416.     record_temp_file (arg, delete_always, delete_failure);
  417. }
  418.  
  419. /* Record the names of temporary files we tell compilers to write,
  420.    and delete them at the end of the run.  */
  421.  
  422. /* This is the common prefix we use to make temp file names.
  423.    It is chosen once for each run of this program.
  424.    It is substituted into a spec by %g.
  425.    Thus, all temp file names contain this prefix.
  426.    In practice, all temp file names start with this prefix.
  427.  
  428.    This prefix comes from the envvar TMPDIR if it is defined;
  429.    otherwise, from the P_tmpdir macro if that is defined;
  430.    otherwise, in /usr/tmp or /tmp.  */
  431.  
  432. char *temp_filename;
  433.  
  434. /* Length of the prefix.  */
  435.  
  436. int temp_filename_length;
  437.  
  438. /* Define the list of temporary files to delete.  */
  439.  
  440. struct temp_file
  441. {
  442.   char *name;
  443.   struct temp_file *next;
  444. };
  445.  
  446. /* Queue of files to delete on success or failure of compilation.  */
  447. struct temp_file *always_delete_queue;
  448. /* Queue of files to delete on failure of compilation.  */
  449. struct temp_file *failure_delete_queue;
  450.  
  451. /* Record FILENAME as a file to be deleted automatically.
  452.    ALWAYS_DELETE nonzero means delete it if all compilation succeeds;
  453.    otherwise delete it in any case.
  454.    FAIL_DELETE nonzero means delete it if a compilation step fails;
  455.    otherwise delete it in any case.  */
  456.  
  457. void
  458. record_temp_file (filename, always_delete, fail_delete)
  459.      char *filename;
  460.      int always_delete;
  461.      int fail_delete;
  462. {
  463.   register char *name;
  464.   name = (char *) xmalloc (strlen (filename) + 1);
  465.   strcpy (name, filename);
  466.  
  467.   if (always_delete)
  468.     {
  469.       register struct temp_file *temp;
  470.       for (temp = always_delete_queue; temp; temp = temp->next)
  471.     if (! strcmp (name, temp->name))
  472.       goto already1;
  473.       temp = (struct temp_file *) xmalloc (sizeof (struct temp_file));
  474.       temp->next = always_delete_queue;
  475.       temp->name = name;
  476.       always_delete_queue = temp;
  477.     already1:;
  478.     }
  479.  
  480.   if (fail_delete)
  481.     {
  482.       register struct temp_file *temp;
  483.       for (temp = failure_delete_queue; temp; temp = temp->next)
  484.     if (! strcmp (name, temp->name))
  485.       goto already2;
  486.       temp = (struct temp_file *) xmalloc (sizeof (struct temp_file));
  487.       temp->next = failure_delete_queue;
  488.       temp->name = name;
  489.       failure_delete_queue = temp;
  490.     already2:;
  491.     }
  492. }
  493.  
  494. /* Delete all the temporary files whose names we previously recorded.  */
  495.  
  496. void
  497. delete_temp_files ()
  498. {
  499.   register struct temp_file *temp;
  500.  
  501.   for (temp = always_delete_queue; temp; temp = temp->next)
  502.     {
  503. #ifdef DEBUG
  504.       int i;
  505.       printf ("Delete %s? (y or n) ", temp->name);
  506.       fflush (stdout);
  507.       i = getchar ();
  508.       if (i != '\n')
  509.     while (getchar () != '\n') ;
  510.       if (i == 'y' || i == 'Y')
  511. #endif /* DEBUG */
  512.     {
  513.       if (unlink (temp->name) < 0)
  514.         if (vflag)
  515.           perror_with_name (temp->name);
  516.     }
  517.     }
  518.  
  519.   always_delete_queue = 0;
  520. }
  521.  
  522. /* Delete all the files to be deleted on error.  */
  523.  
  524. void
  525. delete_failure_queue ()
  526. {
  527.   register struct temp_file *temp;
  528.  
  529.   for (temp = failure_delete_queue; temp; temp = temp->next)
  530.     {
  531. #ifdef DEBUG
  532.       int i;
  533.       printf ("Delete %s? (y or n) ", temp->name);
  534.       fflush (stdout);
  535.       i = getchar ();
  536.       if (i != '\n')
  537.     while (getchar () != '\n') ;
  538.       if (i == 'y' || i == 'Y')
  539. #endif /* DEBUG */
  540.     {
  541.       if (unlink (temp->name) < 0)
  542.         if (vflag)
  543.           perror_with_name (temp->name);
  544.     }
  545.     }
  546. }
  547.  
  548. void
  549. clear_failure_queue ()
  550. {
  551.   failure_delete_queue = 0;
  552. }
  553.  
  554. /* Compute a string to use as the base of all temporary file names.
  555.    It is substituted for %g.  */
  556.  
  557. void
  558. choose_temp_base ()
  559. {
  560.   extern char *getenv ();
  561.   char *base = getenv ("TMPDIR");
  562.   int len;
  563.  
  564.   if (base == (char *)0)
  565.     {
  566. #ifdef P_tmpdir
  567.       if (access (P_tmpdir, R_OK | W_OK) == 0)
  568.     base = P_tmpdir;
  569. #endif
  570.       if (base == (char *)0)
  571.     {
  572.       if (access ("/usr/tmp", R_OK | W_OK) == 0)
  573.         base = "/usr/tmp/";
  574.       else
  575.         base = "/tmp/";
  576.     }
  577.     }
  578.  
  579.   len = strlen (base);
  580.   temp_filename = (char *) xmalloc (len + sizeof("/ccXXXXXX"));
  581.   strcpy (temp_filename, base);
  582.   if (len > 0 && temp_filename[len-1] != '/')
  583.     temp_filename[len++] = '/';
  584.   strcpy (temp_filename + len, "ccXXXXXX");
  585.  
  586.   mktemp (temp_filename);
  587.   temp_filename_length = strlen (temp_filename);
  588. }
  589.  
  590. /* Search for an execute file through our search path.
  591.    Return 0 if not found, otherwise return its name, allocated with malloc.  */
  592.  
  593. static char *
  594. find_exec_file (prog)
  595.      char *prog;
  596. {
  597.   int win = 0;
  598.   char *temp;
  599.   int size;
  600.  
  601.   size = strlen (standard_exec_prefix);
  602.   if (user_exec_prefix != 0 && strlen (user_exec_prefix) > size)
  603.     size = strlen (user_exec_prefix);
  604.   if (env_exec_prefix != 0 && strlen (env_exec_prefix) > size)
  605.     size = strlen (env_exec_prefix);
  606.   if (strlen (standard_exec_prefix_1) > size)
  607.     size = strlen (standard_exec_prefix_1);
  608.   size += strlen (prog) + 1;
  609.   if (machine_suffix)
  610.     size += strlen (machine_suffix) + 1;
  611.   temp = (char *) xmalloc (size);
  612.  
  613.   /* Determine the filename to execute.  */
  614.  
  615.   if (user_exec_prefix)
  616.     {
  617.       if (machine_suffix)
  618.     {
  619.       strcpy (temp, user_exec_prefix);
  620.       strcat (temp, machine_suffix);
  621.       strcat (temp, prog);
  622.       win = (access (temp, X_OK) == 0);
  623.     }
  624.       if (!win)
  625.     {
  626.       strcpy (temp, user_exec_prefix);
  627.       strcat (temp, prog);
  628.       win = (access (temp, X_OK) == 0);
  629.     }
  630.       user_exec_prefix_used |= win;
  631.     }
  632.  
  633.   if (!win && env_exec_prefix)
  634.     {
  635.       if (machine_suffix)
  636.     {
  637.       strcpy (temp, env_exec_prefix);
  638.       strcat (temp, machine_suffix);
  639.       strcat (temp, prog);
  640.       win = (access (temp, X_OK) == 0);
  641.     }
  642.       if (!win)
  643.     {
  644.       strcpy (temp, env_exec_prefix);
  645.       strcat (temp, prog);
  646.       win = (access (temp, X_OK) == 0);
  647.     }
  648.     }
  649.  
  650.   if (!win)
  651.     {
  652.       if (machine_suffix)
  653.     {
  654.       strcpy (temp, standard_exec_prefix);
  655.       strcat (temp, machine_suffix);
  656.       strcat (temp, prog);
  657.       win = (access (temp, X_OK) == 0);
  658.     }
  659.       if (!win)
  660.     {
  661.       strcpy (temp, standard_exec_prefix);
  662.       strcat (temp, prog);
  663.       win = (access (temp, X_OK) == 0);
  664.     }
  665.     }
  666.  
  667.   if (!win)
  668.     {
  669.       if (machine_suffix)
  670.     {
  671.       strcpy (temp, standard_exec_prefix_1);
  672.       strcat (temp, machine_suffix);
  673.       strcat (temp, prog);
  674.       win = (access (temp, X_OK) == 0);
  675.     }
  676.       if (!win)
  677.     {
  678.       strcpy (temp, standard_exec_prefix_1);
  679.       strcat (temp, prog);
  680.       win = (access (temp, X_OK) == 0);
  681.     }
  682.     }
  683.  
  684.   if (win)
  685.     return temp;
  686.   else
  687.     return 0;
  688. }
  689.  
  690. /* stdin file number.  */
  691. #define STDIN_FILE_NO 0
  692.  
  693. /* stdout file number.  */
  694. #define STDOUT_FILE_NO 1
  695.  
  696. /* value of `pipe': port index for reading.  */
  697. #define READ_PORT 0
  698.  
  699. /* value of `pipe': port index for writing.  */
  700. #define WRITE_PORT 1
  701.  
  702. /* Pipe waiting from last process, to be used as input for the next one.
  703.    Value is STDIN_FILE_NO if no pipe is waiting
  704.    (i.e. the next command is the first of a group).  */
  705.  
  706. int last_pipe_input;
  707.  
  708. /* Fork one piped subcommand.  FUNC is the system call to use
  709.    (either execv or execvp).  ARGV is the arg vector to use.
  710.    NOT_LAST is nonzero if this is not the last subcommand
  711.    (i.e. its output should be piped to the next one.)  */
  712.  
  713. static int
  714. pexecute (func, program, argv, not_last)
  715.      char *program;
  716.      int (*func)();
  717.      char *argv[];
  718.      int not_last;
  719. {
  720.   int pid;
  721.   int pdes[2];
  722.   int input_desc = last_pipe_input;
  723.   int output_desc = STDOUT_FILE_NO;
  724.  
  725.   /* If this isn't the last process, make a pipe for its output,
  726.      and record it as waiting to be the input to the next process.  */
  727.  
  728.   if (not_last)
  729.     {
  730.       if (pipe (pdes) < 0)
  731.     pfatal_with_name ("pipe");
  732.       output_desc = pdes[WRITE_PORT];
  733.       last_pipe_input = pdes[READ_PORT];
  734.     }
  735.   else
  736.     last_pipe_input = STDIN_FILE_NO;
  737.  
  738.   pid = vfork ();
  739.  
  740.   switch (pid)
  741.     {
  742.     case -1:
  743.       pfatal_with_name ("vfork");
  744.       break;
  745.  
  746.     case 0: /* child */
  747.       /* Move the input and output pipes into place, if nec.  */
  748.       if (input_desc != STDIN_FILE_NO)
  749.     {
  750.       close (STDIN_FILE_NO);
  751.       dup (input_desc);
  752.       close (input_desc);
  753.     }
  754.       if (output_desc != STDOUT_FILE_NO)
  755.     {
  756.       close (STDOUT_FILE_NO);
  757.       dup (output_desc);
  758.       close (output_desc);
  759.     }
  760.  
  761.       /* Close the parent's descs that aren't wanted here.  */
  762.       if (last_pipe_input != STDIN_FILE_NO)
  763.     close (last_pipe_input);
  764.  
  765.       /* Exec the program.  */
  766.       (*func) (program, argv);
  767.       perror_exec (program);
  768.       exit (-1);
  769.       /* NOTREACHED */
  770.  
  771.     default:
  772.       /* In the parent, after forking.
  773.      Close the descriptors that we made for this child.  */
  774.       if (input_desc != STDIN_FILE_NO)
  775.     close (input_desc);
  776.       if (output_desc != STDOUT_FILE_NO)
  777.     close (output_desc);
  778.  
  779.       /* Return child's process number.  */
  780.       return pid;
  781.     }
  782. }
  783.  
  784. /* Execute the command specified by the arguments on the current line of spec.
  785.    When using pipes, this includes several piped-together commands
  786.    with `|' between them.
  787.  
  788.    Return 0 if successful, -1 if failed.  */
  789.  
  790. int
  791. execute ()
  792. {
  793.   int i;
  794.   int n_commands;        /* # of command.  */
  795.   char *string;
  796.   struct command
  797.     {
  798.       char *prog;        /* program name.  */
  799.       char **argv;        /* vector of args.  */
  800.       int pid;            /* pid of process for this command.  */
  801.     };
  802.  
  803.   struct command *commands;    /* each command buffer with above info.  */
  804.  
  805.   /* Count # of piped commands.  */
  806.   for (n_commands = 1, i = 0; i < argbuf_index; i++)
  807.     if (strcmp (argbuf[i], "|") == 0)
  808.       n_commands++;
  809.  
  810.   /* Get storage for each command.  */
  811.   commands
  812.     = (struct command *) alloca (n_commands * sizeof (struct command));
  813.  
  814.   /* Split argbuf into its separate piped processes,
  815.      and record info about each one.
  816.      Also search for the programs that are to be run.  */
  817.  
  818.   commands[0].prog = argbuf[0]; /* first command.  */
  819.   commands[0].argv = &argbuf[0];
  820.   string = find_exec_file (commands[0].prog);
  821.   if (string)
  822.     commands[0].argv[0] = string;
  823.  
  824.   for (n_commands = 1, i = 0; i < argbuf_index; i++)
  825.     if (strcmp (argbuf[i], "|") == 0)
  826.       {                /* each command.  */
  827.     argbuf[i] = 0;    /* termination of command args.  */
  828.     commands[n_commands].prog = argbuf[i + 1];
  829.     commands[n_commands].argv = &argbuf[i + 1];
  830.     string = find_exec_file (commands[n_commands].prog);
  831.     if (string)
  832.       commands[n_commands].argv[0] = string;
  833.     n_commands++;
  834.       }
  835.  
  836.   argbuf[argbuf_index] = 0;
  837.  
  838.   /* If -v, print what we are about to do, and maybe query.  */
  839.  
  840.   if (vflag)
  841.     {
  842.       /* Print each piped command as a separate line.  */
  843.       for (i = 0; i < n_commands ; i++)
  844.     {
  845.       char **j;
  846.  
  847.       for (j = commands[i].argv; *j; j++)
  848.         fprintf (stderr, " %s", *j);
  849.  
  850.       /* Print a pipe symbol after all but the last command.  */
  851.       if (i + 1 != n_commands)
  852.         fprintf (stderr, " |");
  853.       fprintf (stderr, "\n");
  854.     }
  855.       fflush (stderr);
  856. #ifdef DEBUG
  857.       fprintf (stderr, "\nGo ahead? (y or n) ");
  858.       fflush (stderr);
  859.       j = getchar ();
  860.       if (j != '\n')
  861.     while (getchar () != '\n') ;
  862.       if (j != 'y' && j != 'Y')
  863.     return 0;
  864. #endif /* DEBUG */
  865.     }
  866.  
  867.   /* Run each piped subprocess.  */
  868.  
  869.   last_pipe_input = STDIN_FILE_NO;
  870.   for (i = 0; i < n_commands; i++)
  871.     {
  872.       extern int execv(), execvp();
  873.       char *string = commands[i].argv[0];
  874.  
  875.       commands[i].pid = pexecute ((string != commands[i].prog ? execv : execvp),
  876.                   string, commands[i].argv,
  877.                   i + 1 < n_commands);
  878.  
  879.       if (string != commands[i].prog)
  880.     free (string);
  881.     }
  882.  
  883.   execution_count++;
  884.  
  885.   /* Wait for all the subprocesses to finish.
  886.      We don't care what order they finish in;
  887.      we know that N_COMMANDS waits will get them all.  */
  888.  
  889.   {
  890.     int ret_code = 0;
  891.  
  892.     for (i = 0; i < n_commands; i++)
  893.       {
  894.     int status;
  895.     int pid;
  896.     char *prog;
  897.  
  898.     pid = wait (&status);
  899.     if (pid < 0)
  900.       abort ();
  901.  
  902.     if (status != 0)
  903.       {
  904.         int j;
  905.         for (j = 0; j < n_commands; j++)
  906.           if (commands[j].pid == pid)
  907.         prog = commands[j].prog;
  908.  
  909.         if ((status & 0x7F) != 0)
  910.           fatal ("Program %s got fatal signal %d.", prog, (status & 0x7F));
  911.         if (((status & 0xFF00) >> 8) >= MIN_FATAL_STATUS)
  912.           ret_code = -1;
  913.       }
  914.       }
  915.     return ret_code;
  916.   }
  917. }
  918.  
  919. /* Find all the switches given to us
  920.    and make a vector describing them.
  921.  
  922.    The elements of the vector a strings, one per switch given.
  923.    If a switch uses the following argument, then the `part1' field
  924.    is the switch itself and the `part2' field is the following argument.
  925.    The `valid' field is nonzero if any spec has looked at this switch;
  926.    if it remains zero at the end of the run, it must be meaningless.
  927.  
  928.    `prefix' is the prefix of this switch which makes it
  929.    recognizable as a switch.  This is almost always "-".  When
  930.    it is NULL, it means that the prefix to this switch is part
  931.    `part1' instead.  */
  932.  
  933. struct switchstr
  934. {
  935.   char *prefix;
  936.   char *part1;
  937.   char *part2;
  938.   int valid;
  939. };
  940.  
  941. struct switchstr *switches;
  942.  
  943. int n_switches;
  944.  
  945. /* Also a vector of input files specified.  */
  946.  
  947. char **infiles;
  948.  
  949. int n_infiles;
  950.  
  951. /* And a vector of corresponding output files is made up later.  */
  952.  
  953. char **outfiles;
  954.  
  955. /* Create the vector `switches' and its contents.
  956.    Store its length in `n_switches'.  */
  957.  
  958. void
  959. process_command (argc, argv)
  960.      int argc;
  961.      char **argv;
  962. {
  963.   extern char *getenv ();
  964.   register int i;
  965.   n_switches = 0;
  966.   n_infiles = 0;
  967.  
  968.   env_exec_prefix = getenv ("GCC_EXEC_PREFIX");
  969.  
  970.   /* Scan argv twice.  Here, the first time, just count how many switches
  971.      there will be in their vector, and how many input files in theirs.
  972.      Here we also parse the switches that cc itself uses (e.g. -v).  */
  973.  
  974.   for (i = 1; i < argc; i++)
  975.     {
  976.       if (argv[i][0] == '-' && argv[i][1] != 'l')
  977.     {
  978.       register char *p = &argv[i][1];
  979.       register int c = *p;
  980.  
  981.       switch (c)
  982.         {
  983.         case 'b':
  984.           machine_suffix = p + 1;
  985.           break;
  986.  
  987.         case 'B':
  988.           user_exec_prefix = p + 1;
  989.           break;
  990.  
  991.         case 'v':    /* Print our subcommands and print versions.  */
  992.           vflag++;
  993.           n_switches++;
  994.           break;
  995.  
  996.         default:
  997.           n_switches++;
  998.  
  999.           if (SWITCH_TAKES_ARG (c) && p[1] == 0)
  1000.         i++;
  1001.           else if (WORD_SWITCH_TAKES_ARG (p))
  1002.         i++;
  1003.         }
  1004.     }
  1005.       else if (argv[i][0] == '+')
  1006.     ;
  1007.       else
  1008.     n_infiles++;
  1009.     }
  1010.  
  1011.   /* Then create the space for the vectors and scan again.  */
  1012.  
  1013.   switches = ((struct switchstr *)
  1014.           xmalloc ((n_switches + 1) * sizeof (struct switchstr)));
  1015.   infiles = (char **) xmalloc ((n_infiles + 1) * sizeof (char *));
  1016.   n_switches = 0;
  1017.   n_infiles = 0;
  1018.  
  1019.   /* This, time, copy the text of each switch and store a pointer
  1020.      to the copy in the vector of switches.
  1021.      Store all the infiles in their vector.  */
  1022.  
  1023.   for (i = 1; i < argc; i++)
  1024.     {
  1025.       if (argv[i][0] == '-' && argv[i][1] != 'l')
  1026.     {
  1027.       register char *p = &argv[i][1];
  1028.       register int c = *p;
  1029.  
  1030.       if (c == 'B' || c == 'b')
  1031.         continue;
  1032.       switches[n_switches].prefix = "-";
  1033.       switches[n_switches].part1 = p;
  1034.       if ((SWITCH_TAKES_ARG (c) && p[1] == 0)
  1035.           || WORD_SWITCH_TAKES_ARG (p))
  1036.         switches[n_switches].part2 = argv[++i];
  1037.       else
  1038.         switches[n_switches].part2 = 0;
  1039.       switches[n_switches].valid = 0;
  1040.       n_switches++;
  1041.     }
  1042.       else if (argv[i][0] == '+')
  1043.     {
  1044.       switches[n_switches].prefix = "";
  1045.       switches[n_switches].part1 = argv[i];
  1046.       switches[n_switches].part2 = 0;
  1047.       switches[n_switches].valid = 0;
  1048.       n_switches++;
  1049.     }
  1050.       else
  1051.     infiles[n_infiles++] = argv[i];
  1052.     }
  1053.  
  1054.   switches[n_switches].part1 = 0;
  1055.   infiles[n_infiles] = 0;
  1056. }
  1057.  
  1058. /* Process a spec string, accumulating and running commands.  */
  1059.  
  1060. /* These variables describe the input file name.
  1061.    input_file_number is the index on outfiles of this file,
  1062.    so that the output file name can be stored for later use by %o.
  1063.    input_basename is the start of the part of the input file
  1064.    sans all directory names, and basename_length is the number
  1065.    of characters starting there excluding the suffix .c or whatever.  */
  1066.  
  1067. char *input_filename;
  1068. int input_file_number;
  1069. int input_filename_length;
  1070. int basename_length;
  1071. char *input_basename;
  1072.  
  1073. /* These are variables used within do_spec and do_spec_1.  */
  1074.  
  1075. /* Nonzero if an arg has been started and not yet terminated
  1076.    (with space, tab or newline).  */
  1077. int arg_going;
  1078.  
  1079. /* Nonzero means %d or %g has been seen; the next arg to be terminated
  1080.    is a temporary file name.  */
  1081. int delete_this_arg;
  1082.  
  1083. /* Nonzero means %w has been seen; the next arg to be terminated
  1084.    is the output file name of this compilation.  */
  1085. int this_is_output_file;
  1086.  
  1087. /* Nonzero means %s has been seen; the next arg to be terminated
  1088.    is the name of a library file and we should try the standard
  1089.    search dirs for it.  */
  1090. int this_is_library_file;
  1091.  
  1092. /* Process the spec SPEC and run the commands specified therein.
  1093.    Returns 0 if the spec is successfully processed; -1 if failed.  */
  1094.  
  1095. int
  1096. do_spec (spec)
  1097.      char *spec;
  1098. {
  1099.   int value;
  1100.  
  1101.   clear_args ();
  1102.   arg_going = 0;
  1103.   delete_this_arg = 0;
  1104.   this_is_output_file = 0;
  1105.   this_is_library_file = 0;
  1106.  
  1107.   value = do_spec_1 (spec, 0);
  1108.  
  1109.   /* Force out any unfinished command.
  1110.      If -pipe, this forces out the last command if it ended in `|'.  */
  1111.   if (value == 0)
  1112.     {
  1113.       if (argbuf_index > 0 && !strcmp (argbuf[argbuf_index - 1], "|"))
  1114.     argbuf_index--;
  1115.  
  1116.       if (argbuf_index > 0)
  1117.     value = execute ();
  1118.     }
  1119.  
  1120.   return value;
  1121. }
  1122.  
  1123. /* Process the sub-spec SPEC as a portion of a larger spec.
  1124.    This is like processing a whole spec except that we do
  1125.    not initialize at the beginning and we do not supply a
  1126.    newline by default at the end.
  1127.    INSWITCH nonzero means don't process %-sequences in SPEC;
  1128.    in this case, % is treated as an ordinary character.
  1129.    This is used while substituting switches.
  1130.    INSWITCH nonzero also causes SPC not to terminate an argument.
  1131.  
  1132.    Value is zero unless a line was finished
  1133.    and the command on that line reported an error.  */
  1134.  
  1135. int
  1136. do_spec_1 (spec, inswitch)
  1137.      char *spec;
  1138.      int inswitch;
  1139. {
  1140.   register char *p = spec;
  1141.   register int c;
  1142.   char *string;
  1143.  
  1144.   while (c = *p++)
  1145.     /* If substituting a switch, treat all chars like letters.
  1146.        Otherwise, NL, SPC, TAB and % are special.  */
  1147.     switch (inswitch ? 'a' : c)
  1148.       {
  1149.       case '\n':
  1150.     /* End of line: finish any pending argument,
  1151.        then run the pending command if one has been started.  */
  1152.     if (arg_going)
  1153.       {
  1154.         obstack_1grow (&obstack, 0);
  1155.         string = obstack_finish (&obstack);
  1156.         if (this_is_library_file)
  1157.           string = find_file (string);
  1158.         store_arg (string, delete_this_arg, this_is_output_file);
  1159.         if (this_is_output_file)
  1160.           outfiles[input_file_number] = string;
  1161.       }
  1162.     arg_going = 0;
  1163.  
  1164.     if (argbuf_index > 0 && !strcmp (argbuf[argbuf_index - 1], "|"))
  1165.       {
  1166.         int i;
  1167.         for (i = 0; i < n_switches; i++)
  1168.           if (!strcmp (switches[i].part1, "pipe"))
  1169.         break;
  1170.  
  1171.         /* A `|' before the newline means use a pipe here,
  1172.            but only if -pipe was specified.
  1173.            Otherwise, execute now and don't pass the `|' as an arg.  */
  1174.         if (i < n_switches)
  1175.           {
  1176.         switches[i].valid = 1;
  1177.         break;
  1178.           }
  1179.         else
  1180.           argbuf_index--;
  1181.       }
  1182.  
  1183.     if (argbuf_index > 0)
  1184.       {
  1185.         int value = execute ();
  1186.         if (value)
  1187.           return value;
  1188.       }
  1189.     /* Reinitialize for a new command, and for a new argument.  */
  1190.     clear_args ();
  1191.     arg_going = 0;
  1192.     delete_this_arg = 0;
  1193.     this_is_output_file = 0;
  1194.     this_is_library_file = 0;
  1195.     break;
  1196.  
  1197.       case '|':
  1198.     /* End any pending argument.  */
  1199.     if (arg_going)
  1200.       {
  1201.         obstack_1grow (&obstack, 0);
  1202.         string = obstack_finish (&obstack);
  1203.         if (this_is_library_file)
  1204.           string = find_file (string);
  1205.         store_arg (string, delete_this_arg, this_is_output_file);
  1206.         if (this_is_output_file)
  1207.           outfiles[input_file_number] = string;
  1208.       }
  1209.  
  1210.     /* Use pipe */
  1211.     obstack_1grow (&obstack, c);
  1212.     arg_going = 1;
  1213.     break;
  1214.  
  1215.       case '\t':
  1216.       case ' ':
  1217.     /* Space or tab ends an argument if one is pending.  */
  1218.     if (arg_going)
  1219.       {
  1220.         obstack_1grow (&obstack, 0);
  1221.         string = obstack_finish (&obstack);
  1222.         if (this_is_library_file)
  1223.           string = find_file (string);
  1224.         store_arg (string, delete_this_arg, this_is_output_file);
  1225.         if (this_is_output_file)
  1226.           outfiles[input_file_number] = string;
  1227.       }
  1228.     /* Reinitialize for a new argument.  */
  1229.     arg_going = 0;
  1230.     delete_this_arg = 0;
  1231.     this_is_output_file = 0;
  1232.     this_is_library_file = 0;
  1233.     break;
  1234.  
  1235.       case '%':
  1236.     switch (c = *p++)
  1237.       {
  1238.       case 0:
  1239.         fatal ("Invalid specification!  Bug in cc.");
  1240.  
  1241.       case 'b':
  1242.         obstack_grow (&obstack, input_basename, basename_length);
  1243.         arg_going = 1;
  1244.         break;
  1245.  
  1246.       case 'd':
  1247.         delete_this_arg = 2;
  1248.         break;
  1249.  
  1250.       case 'e':
  1251.         /* {...:%efoo} means report an error with `foo' as error message
  1252.            and don't execute any more commands for this file.  */
  1253.         {
  1254.           char *q = p;
  1255.           char *buf;
  1256.           while (*p != 0 && *p != '\n') p++;
  1257.           buf = (char *) alloca (p - q + 1);
  1258.           strncpy (buf, q, p - q);
  1259.           error ("%s", buf);
  1260.           return -1;
  1261.         }
  1262.         break;
  1263.  
  1264.       case 'g':
  1265.         obstack_grow (&obstack, temp_filename, temp_filename_length);
  1266.         delete_this_arg = 1;
  1267.         arg_going = 1;
  1268.         break;
  1269.  
  1270.       case 'i':
  1271.         obstack_grow (&obstack, input_filename, input_filename_length);
  1272.         arg_going = 1;
  1273.         break;
  1274.  
  1275.       case 'o':
  1276.         {
  1277.           register int f;
  1278.           for (f = 0; f < n_infiles; f++)
  1279.         store_arg (outfiles[f], 0, 0);
  1280.         }
  1281.         break;
  1282.  
  1283.  
  1284.       case 'r':
  1285.         obstack_grow (&obstack, standard_startfile_prefix,
  1286.               strlen(standard_startfile_prefix));
  1287.         break;
  1288.  
  1289.  
  1290.       case 's':
  1291.         this_is_library_file = 1;
  1292.         break;
  1293.  
  1294.       case 'w':
  1295.         this_is_output_file = 1;
  1296.         break;
  1297.  
  1298.       case '{':
  1299.         p = handle_braces (p);
  1300.         if (p == 0)
  1301.           return -1;
  1302.         break;
  1303.  
  1304.       case '%':
  1305.         obstack_1grow (&obstack, '%');
  1306.         break;
  1307.  
  1308. /*** The rest just process a certain constant string as a spec.  */
  1309.  
  1310.       case '1':
  1311.         do_spec_1 (CC1_SPEC, 0);
  1312.         break;
  1313.  
  1314.       case 'a':
  1315.         do_spec_1 (ASM_SPEC, 0);
  1316.         break;
  1317.  
  1318.       case 'c':
  1319.         do_spec_1 (SIGNED_CHAR_SPEC, 0);
  1320.         break;
  1321.  
  1322.       case 'C':
  1323.         do_spec_1 (CPP_SPEC, 0);
  1324.         break;
  1325.  
  1326.       case 'l':
  1327.         do_spec_1 (LINK_SPEC, 0);
  1328.         break;
  1329.  
  1330.       case 'L':
  1331.         do_spec_1 (LIB_SPEC, 0);
  1332.         break;
  1333.  
  1334.       case 'p':
  1335.         do_spec_1 (CPP_PREDEFINES, 0);
  1336.         break;
  1337.  
  1338.       case 'P':
  1339.         {
  1340.           char *x = (char *) alloca (strlen (CPP_PREDEFINES) * 2 + 1);
  1341.           char *buf = x;
  1342.           char *y = CPP_PREDEFINES;
  1343.  
  1344.           /* Copy all of CPP_PREDEFINES into BUF,
  1345.          but put __ after every -D and at the end of each arg,  */
  1346.           while (1)
  1347.         {
  1348.           if (! strncmp (y, "-D", 2))
  1349.             {
  1350.               *x++ = '-';
  1351.               *x++ = 'D';
  1352.               *x++ = '_';
  1353.               *x++ = '_';
  1354.               y += 2;
  1355.             }
  1356.           else if (*y == ' ' || *y == 0)
  1357.             {
  1358.               *x++ = '_';
  1359.               *x++ = '_';
  1360.               if (*y == 0)
  1361.             break;
  1362.               else
  1363.             *x++ = *y++;
  1364.             }
  1365.           else
  1366.             *x++ = *y++;
  1367.         }
  1368.           *x = 0;
  1369.  
  1370.           do_spec_1 (buf, 0);
  1371.         }
  1372.         break;
  1373.  
  1374.       case 'S':
  1375.         do_spec_1 (STARTFILE_SPEC, 0);
  1376.         break;
  1377.  
  1378.       default:
  1379.         abort ();
  1380.       }
  1381.     break;
  1382.  
  1383.       default:
  1384.     /* Ordinary character: put it into the current argument.  */
  1385.     obstack_1grow (&obstack, c);
  1386.     arg_going = 1;
  1387.       }
  1388.  
  1389.   return 0;        /* End of string */
  1390. }
  1391.  
  1392. /* Return 0 if we call do_spec_1 and that returns -1.  */
  1393.  
  1394. char *
  1395. handle_braces (p)
  1396.      register char *p;
  1397. {
  1398.   register char *q;
  1399.   char *filter;
  1400.   int pipe = 0;
  1401.   int negate = 0;
  1402.  
  1403.   if (*p == '|')
  1404.     /* A `|' after the open-brace means,
  1405.        if the test fails, output a single minus sign rather than nothing.
  1406.        This is used in %{|!pipe:...}.  */
  1407.     pipe = 1, ++p;
  1408.  
  1409.   if (*p == '!')
  1410.     /* A `!' after the open-brace negates the condition:
  1411.        succeed if the specified switch is not present.  */
  1412.     negate = 1, ++p;
  1413.  
  1414.   filter = p;
  1415.   while (*p != ':' && *p != '}') p++;
  1416.   if (*p != '}')
  1417.     {
  1418.       register int count = 1;
  1419.       q = p + 1;
  1420.       while (count > 0)
  1421.     {
  1422.       if (*q == '{')
  1423.         count++;
  1424.       else if (*q == '}')
  1425.         count--;
  1426.       else if (*q == 0)
  1427.         abort ();
  1428.       q++;
  1429.     }
  1430.     }
  1431.   else
  1432.     q = p + 1;
  1433.  
  1434.   if (p[-1] == '*' && p[0] == '}')
  1435.     {
  1436.       /* Substitute all matching switches as separate args.  */
  1437.       register int i;
  1438.       --p;
  1439.       for (i = 0; i < n_switches; i++)
  1440.     if (!strncmp (switches[i].part1, filter, p - filter))
  1441.       give_switch (i);
  1442.     }
  1443.   else
  1444.     {
  1445.       /* Test for presence of the specified switch.  */
  1446.       register int i;
  1447.       int present = 0;
  1448.  
  1449.       /* If name specified ends in *, as in {x*:...},
  1450.      check for presence of any switch name starting with x.  */
  1451.       if (p[-1] == '*')
  1452.     {
  1453.       for (i = 0; i < n_switches; i++)
  1454.         {
  1455.           if (!strncmp (switches[i].part1, filter, p - filter - 1))
  1456.         {
  1457.           switches[i].valid = 1;
  1458.           present = 1;
  1459.         }
  1460.         }
  1461.     }
  1462.       /* Otherwise, check for presence of exact name specified.  */
  1463.       else
  1464.     {
  1465.       for (i = 0; i < n_switches; i++)
  1466.         {
  1467.           if (!strncmp (switches[i].part1, filter, p - filter)
  1468.           && switches[i].part1[p - filter] == 0)
  1469.         {
  1470.           switches[i].valid = 1;
  1471.           present = 1;
  1472.           break;
  1473.         }
  1474.         }
  1475.     }
  1476.  
  1477.       /* If it is as desired (present for %{s...}, absent for %{-s...})
  1478.      then substitute either the switch or the specified
  1479.      conditional text.  */
  1480.       if (present != negate)
  1481.     {
  1482.       if (*p == '}')
  1483.         {
  1484.           give_switch (i);
  1485.         }
  1486.       else
  1487.         {
  1488.           if (do_spec_1 (save_string (p + 1, q - p - 2), 0) < 0)
  1489.         return 0;
  1490.         }
  1491.     }
  1492.       else if (pipe)
  1493.     {
  1494.       /* Here if a %{|...} conditional fails: output a minus sign,
  1495.          which means "standard output" or "standard input".  */
  1496.       do_spec_1 ("-", 0);
  1497.     }
  1498.     }
  1499.  
  1500.   return q;
  1501. }
  1502.  
  1503. /* Pass a switch to the current accumulating command
  1504.    in the same form that we received it.
  1505.    SWITCHNUM identifies the switch; it is an index into
  1506.    the vector of switches gcc received, which is `switches'.
  1507.    This cannot fail since it never finishes a command line.  */
  1508.  
  1509. give_switch (switchnum)
  1510.      int switchnum;
  1511. {
  1512.   do_spec_1 (switches[switchnum].prefix, 1);
  1513.   do_spec_1 (switches[switchnum].part1, 1);
  1514.   do_spec_1 (" ", 0);
  1515.   if (switches[switchnum].part2 != 0)
  1516.     {
  1517.       do_spec_1 (switches[switchnum].part2, 1);
  1518.       do_spec_1 (" ", 0);
  1519.     }
  1520.   switches[switchnum].valid = 1;
  1521. }
  1522.  
  1523. /* Search for a file named NAME trying various prefixes including the
  1524.    user's -B prefix and some standard ones.
  1525.    Return the absolute pathname found.  If nothing is found, return NAME.  */
  1526.  
  1527. char *
  1528. find_file (name)
  1529.      char *name;
  1530. {
  1531.   int size;
  1532.   char *temp;
  1533.   int win = 0;
  1534.  
  1535.   /* Compute maximum size of NAME plus any prefix we will try.  */
  1536.  
  1537.   size = strlen (standard_exec_prefix);
  1538.   if (user_exec_prefix != 0 && strlen (user_exec_prefix) > size)
  1539.     size = strlen (user_exec_prefix);
  1540.   if (env_exec_prefix != 0 && strlen (env_exec_prefix) > size)
  1541.     size = strlen (env_exec_prefix);
  1542.   if (strlen (standard_exec_prefix) > size)
  1543.     size = strlen (standard_exec_prefix);
  1544.   if (strlen (standard_exec_prefix_1) > size)
  1545.     size = strlen (standard_exec_prefix_1);
  1546.   if (strlen (standard_startfile_prefix) > size)
  1547.     size = strlen (standard_startfile_prefix);
  1548.   if (strlen (standard_startfile_prefix_1) > size)
  1549.     size = strlen (standard_startfile_prefix_1);
  1550.   if (strlen (standard_startfile_prefix_2) > size)
  1551.     size = strlen (standard_startfile_prefix_2);
  1552.   if (machine_suffix)
  1553.     size += strlen (machine_suffix) + 1;
  1554.   size += strlen (name) + 1;
  1555.  
  1556.   temp = (char *) alloca (size);
  1557.  
  1558.   if (user_exec_prefix)
  1559.     {
  1560.       if (machine_suffix)
  1561.     {
  1562.       strcpy (temp, user_exec_prefix);
  1563.       strcat (temp, machine_suffix);
  1564.       strcat (temp, name);
  1565.       win = (access (temp, R_OK) == 0);
  1566.     }
  1567.       if (!win)
  1568.     {
  1569.       strcpy (temp, user_exec_prefix);
  1570.       strcat (temp, name);
  1571.       win = (access (temp, R_OK) == 0);
  1572.     }
  1573.     }
  1574.  
  1575.   if (!win && env_exec_prefix)
  1576.     {
  1577.       if (machine_suffix)
  1578.     {
  1579.       strcpy (temp, env_exec_prefix);
  1580.       strcat (temp, machine_suffix);
  1581.       strcat (temp, name);
  1582.       win = (access (temp, R_OK) == 0);
  1583.     }
  1584.       if (!win)
  1585.     {
  1586.       strcpy (temp, env_exec_prefix);
  1587.       strcat (temp, name);
  1588.       win = (access (temp, R_OK) == 0);
  1589.     }
  1590.     }
  1591.  
  1592.   if (!win)
  1593.     {
  1594.       if (machine_suffix)
  1595.     {
  1596.       strcpy (temp, standard_exec_prefix);
  1597.       strcat (temp, machine_suffix);
  1598.       strcat (temp, name);
  1599.       win = (access (temp, R_OK) == 0);
  1600.     }
  1601.       if (!win)
  1602.     {
  1603.       strcpy (temp, standard_exec_prefix);
  1604.       strcat (temp, name);
  1605.       win = (access (temp, R_OK) == 0);
  1606.     }
  1607.     }
  1608.  
  1609.   if (!win)
  1610.     {
  1611.       if (machine_suffix)
  1612.     {
  1613.       strcpy (temp, standard_exec_prefix_1);
  1614.       strcat (temp, machine_suffix);
  1615.       strcat (temp, name);
  1616.       win = (access (temp, R_OK) == 0);
  1617.     }
  1618.       if (!win)
  1619.     {
  1620.       strcpy (temp, standard_exec_prefix_1);
  1621.       strcat (temp, name);
  1622.       win = (access (temp, R_OK) == 0);
  1623.     }
  1624.     }
  1625.  
  1626.   if (!win)
  1627.     {
  1628.       if (machine_suffix)
  1629.     {
  1630.       strcpy (temp, standard_startfile_prefix);
  1631.       strcat (temp, machine_suffix);
  1632.       strcat (temp, name);
  1633.       win = (access (temp, R_OK) == 0);
  1634.     }
  1635.       if (!win)
  1636.     {
  1637.       strcpy (temp, standard_startfile_prefix);
  1638.       strcat (temp, name);
  1639.       win = (access (temp, R_OK) == 0);
  1640.     }
  1641.     }
  1642.  
  1643.   if (!win)
  1644.     {
  1645.       if (machine_suffix)
  1646.     {
  1647.       strcpy (temp, standard_startfile_prefix_1);
  1648.       strcat (temp, machine_suffix);
  1649.       strcat (temp, name);
  1650.       win = (access (temp, R_OK) == 0);
  1651.     }
  1652.       if (!win)
  1653.     {
  1654.       strcpy (temp, standard_startfile_prefix_1);
  1655.       strcat (temp, name);
  1656.       win = (access (temp, R_OK) == 0);
  1657.     }
  1658.     }
  1659.  
  1660.   if (!win)
  1661.     {
  1662.       if (machine_suffix)
  1663.     {
  1664.       strcpy (temp, standard_startfile_prefix_2);
  1665.       strcat (temp, machine_suffix);
  1666.       strcat (temp, name);
  1667.       win = (access (temp, R_OK) == 0);
  1668.     }
  1669.       if (!win)
  1670.     {
  1671.       strcpy (temp, standard_startfile_prefix_2);
  1672.       strcat (temp, name);
  1673.       win = (access (temp, R_OK) == 0);
  1674.     }
  1675.     }
  1676.  
  1677.   if (!win)
  1678.     {
  1679.       if (machine_suffix)
  1680.     {
  1681.       strcpy (temp, "./");
  1682.       strcat (temp, machine_suffix);
  1683.       strcat (temp, name);
  1684.       win = (access (temp, R_OK) == 0);
  1685.     }
  1686.       if (!win)
  1687.     {
  1688.       strcpy (temp, "./");
  1689.       strcat (temp, name);
  1690.       win = (access (temp, R_OK) == 0);
  1691.     }
  1692.     }
  1693.  
  1694.   if (win)
  1695.     return save_string (temp, strlen (temp));
  1696.   return name;
  1697. }
  1698.  
  1699. /* On fatal signals, delete all the temporary files.  */
  1700.  
  1701. void
  1702. fatal_error (signum)
  1703.      int signum;
  1704. {
  1705.   signal (signum, SIG_DFL);
  1706.   delete_failure_queue ();
  1707.   delete_temp_files ();
  1708.   /* Get the same signal again, this time not handled,
  1709.      so its normal effect occurs.  */
  1710.   kill (getpid (), signum);
  1711. }
  1712.  
  1713. int
  1714. main (argc, argv)
  1715.      int argc;
  1716.      char **argv;
  1717. {
  1718.   register int i;
  1719.   int value;
  1720.   int error_count = 0;
  1721.   int linker_was_run = 0;
  1722.   char *explicit_link_files;
  1723.  
  1724.   programname = argv[0];
  1725.  
  1726.   if (signal (SIGINT, SIG_IGN) != SIG_IGN)
  1727.     signal (SIGINT, fatal_error);
  1728.   if (signal (SIGHUP, SIG_IGN) != SIG_IGN)
  1729.     signal (SIGHUP, fatal_error);
  1730.   if (signal (SIGTERM, SIG_IGN) != SIG_IGN)
  1731.     signal (SIGTERM, fatal_error);
  1732.  
  1733.   argbuf_length = 10;
  1734.   argbuf = (char **) xmalloc (argbuf_length * sizeof (char *));
  1735.  
  1736.   obstack_init (&obstack);
  1737.  
  1738.   choose_temp_base ();
  1739.  
  1740.   /* Make a table of what switches there are (switches, n_switches).
  1741.      Make a table of specified input files (infiles, n_infiles).  */
  1742.  
  1743.   process_command (argc, argv);
  1744.  
  1745.   if (vflag)
  1746.     {
  1747.       extern char *version_string;
  1748.       fprintf (stderr, "g++ version %s\n", version_string);
  1749.       if (n_infiles == 0)
  1750.     exit (0);
  1751.     }
  1752.  
  1753.   if (n_infiles == 0)
  1754.     fatal ("No input files specified.");
  1755.  
  1756.   /* Make a place to record the compiler output file names
  1757.      that correspond to the input files.  */
  1758.  
  1759.   outfiles = (char **) xmalloc (n_infiles * sizeof (char *));
  1760.   bzero (outfiles, n_infiles * sizeof (char *));
  1761.  
  1762.   /* Record which files were specified explicitly as link input.  */
  1763.  
  1764.   explicit_link_files = (char *) xmalloc (n_infiles);
  1765.   bzero (explicit_link_files, n_infiles);
  1766.  
  1767.   for (i = 0; i < n_infiles; i++)
  1768.     {
  1769.       register struct compiler *cp;
  1770.       int this_file_error = 0;
  1771.  
  1772.       /* Tell do_spec what to substitute for %i.  */
  1773.  
  1774.       input_filename = infiles[i];
  1775.       input_filename_length = strlen (input_filename);
  1776.       input_file_number = i;
  1777.  
  1778.       /* Use the same thing in %o, unless cp->spec says otherwise.  */
  1779.  
  1780.       outfiles[i] = input_filename;
  1781.  
  1782.       /* Figure out which compiler from the file's suffix.  */
  1783.  
  1784.       for (cp = compilers; cp->spec; cp++)
  1785.     {
  1786.       if (strlen (cp->suffix) < input_filename_length
  1787.           && !strcmp (cp->suffix,
  1788.               infiles[i] + input_filename_length
  1789.               - strlen (cp->suffix)))
  1790.         {
  1791.           /* Ok, we found an applicable compiler.  Run its spec.  */
  1792.           /* First say how much of input_filename to substitute for %b  */
  1793.           register char *p;
  1794.  
  1795.           input_basename = input_filename;
  1796.           for (p = input_filename; *p; p++)
  1797.         if (*p == '/')
  1798.           input_basename = p + 1;
  1799.           basename_length = (input_filename_length - strlen (cp->suffix)
  1800.                  - (input_basename - input_filename));
  1801.           value = do_spec (cp->spec);
  1802.           if (value < 0)
  1803.         this_file_error = 1;
  1804.           else
  1805.         if (user_exec_prefix != 0
  1806.             && user_exec_prefix_used == 0)
  1807.           fprintf (stderr, "`-B%s' not used by compiler\n", user_exec_prefix);
  1808.           break;
  1809.         }
  1810.     }
  1811.  
  1812.       /* If this file's name does not contain a recognized suffix,
  1813.      record it as explicit linker input.  */
  1814.  
  1815.       if (! cp->spec)
  1816.     explicit_link_files[i] = 1;
  1817.  
  1818.       /* Clear the delete-on-failure queue, deleting the files in it
  1819.      if this compilation failed.  */
  1820.  
  1821.       if (this_file_error)
  1822.     {
  1823.       delete_failure_queue ();
  1824.       error_count++;
  1825.     }
  1826.       /* If this compilation succeeded, don't delete those files later.  */
  1827.       clear_failure_queue ();
  1828.     }
  1829.  
  1830.   /* Run ld to link all the compiler output files.  */
  1831.  
  1832.   if (error_count == 0)
  1833.     {
  1834.       int tmp = execution_count;
  1835.       value = do_spec (link_spec);
  1836.       if (value < 0)
  1837.     error_count = 1;
  1838.       linker_was_run = (tmp != execution_count);
  1839.     }
  1840.  
  1841. #ifdef USE_COLLECT
  1842.   /* C++: now collect all the requirements for
  1843.      calling global constructors and destructors,
  1844.      and write them to a file.  We will link this file
  1845.      in when we run.  */
  1846.   if (linker_was_run && ! error_count)
  1847.     {
  1848.       int tmp = execution_count;
  1849.       value = do_spec (collect_spec);
  1850.       if (value < 0)
  1851.     error_count = 1;
  1852.       linker_was_run &= (tmp != execution_count);
  1853.     }
  1854. #endif
  1855.  
  1856.   /* If options said don't run linker,
  1857.      complain about input files to be given to the linker.  */
  1858.  
  1859.   if (! linker_was_run && error_count == 0)
  1860.     for (i = 0; i < n_infiles; i++)
  1861.       if (explicit_link_files[i])
  1862.     error ("%s: linker input file unused since linking not done",
  1863.            outfiles[i]);
  1864.  
  1865.   /* Set the `valid' bits for switches that match anything in any spec.  */
  1866.  
  1867.   validate_all_switches ();
  1868.  
  1869.   /* Warn about any switches that no pass was interested in.  */
  1870.   
  1871.   for (i = 0; i < n_switches; i++)
  1872.     if (! switches[i].valid)
  1873.       error ("unrecognized option `%s%s'",
  1874.          switches[i].prefix, switches[i].part1);
  1875.  
  1876.   /* Delete some or all of the temporary files we made.  */
  1877.  
  1878.   if (error_count)
  1879.     delete_failure_queue ();
  1880.   delete_temp_files ();
  1881.  
  1882.   exit (error_count);
  1883. }
  1884.  
  1885. xmalloc (size)
  1886.      int size;
  1887. {
  1888.   register int value = malloc (size);
  1889.   if (value == 0)
  1890.     fatal ("Virtual memory full.");
  1891.   return value;
  1892. }
  1893.  
  1894. xrealloc (ptr, size)
  1895.      int ptr, size;
  1896. {
  1897.   register int value = realloc (ptr, size);
  1898.   if (value == 0)
  1899.     fatal ("Virtual memory full.");
  1900.   return value;
  1901. }
  1902.  
  1903. /* Return a newly-allocated string whose contents concatenate those of s1, s2, s3.  */
  1904.  
  1905. char *
  1906. concat (s1, s2, s3)
  1907.      char *s1, *s2, *s3;
  1908. {
  1909.   int len1 = strlen (s1), len2 = strlen (s2), len3 = strlen (s3);
  1910.   char *result = (char *) xmalloc (len1 + len2 + len3 + 1);
  1911.  
  1912.   strcpy (result, s1);
  1913.   strcpy (result + len1, s2);
  1914.   strcpy (result + len1 + len2, s3);
  1915.   *(result + len1 + len2 + len3) = 0;
  1916.  
  1917.   return result;
  1918. }
  1919.  
  1920. char *
  1921. save_string (s, len)
  1922.      char *s;
  1923.      int len;
  1924. {
  1925.   register char *result = (char *) xmalloc (len + 1);
  1926.  
  1927.   bcopy (s, result, len);
  1928.   result[len] = 0;
  1929.   return result;
  1930. }
  1931.  
  1932. pfatal_with_name (name)
  1933.      char *name;
  1934. {
  1935.   extern int errno, sys_nerr;
  1936.   extern char *sys_errlist[];
  1937.   char *s;
  1938.  
  1939.   if (errno < sys_nerr)
  1940.     s = concat ("%s: ", sys_errlist[errno], "");
  1941.   else
  1942.     s = "cannot open %s";
  1943.   fatal (s, name);
  1944. }
  1945.  
  1946. perror_with_name (name)
  1947.      char *name;
  1948. {
  1949.   extern int errno, sys_nerr;
  1950.   extern char *sys_errlist[];
  1951.   char *s;
  1952.  
  1953.   if (errno < sys_nerr)
  1954.     s = concat ("%s: ", sys_errlist[errno], "");
  1955.   else
  1956.     s = "cannot open %s";
  1957.   error (s, name);
  1958. }
  1959.  
  1960. perror_exec (name)
  1961.      char *name;
  1962. {
  1963.   extern int errno, sys_nerr;
  1964.   extern char *sys_errlist[];
  1965.   char *s;
  1966.  
  1967.   if (errno < sys_nerr)
  1968.     s = concat ("installation problem, cannot exec %s: ",
  1969.         sys_errlist[errno], "");
  1970.   else
  1971.     s = "installation problem, cannot exec %s";
  1972.   error (s, name);
  1973. }
  1974.  
  1975. /* More 'friendly' abort that prints the line and file.
  1976.    config.h can #define abort fancy_abort if you like that sort of thing.  */
  1977.  
  1978. void
  1979. fancy_abort ()
  1980. {
  1981.   fatal ("Internal gcc abort.");
  1982. }
  1983.  
  1984. #ifdef HAVE_VPRINTF
  1985.  
  1986. /* Output an error message and exit */
  1987.  
  1988. int 
  1989. fatal (va_alist)
  1990.      va_dcl
  1991. {
  1992.   va_list ap;
  1993.   char *format;
  1994.   
  1995.   va_start(ap);
  1996.   format = va_arg (ap, char *);
  1997.   vfprintf (stderr, format, ap);
  1998.   va_end (ap);
  1999.   fprintf (stderr, "\n");
  2000.   delete_temp_files (0);
  2001.   exit (1);
  2002. }  
  2003.  
  2004. error (va_alist) 
  2005.      va_dcl
  2006. {
  2007.   va_list ap;
  2008.   char *format;
  2009.  
  2010.   va_start(ap);
  2011.   format = va_arg (ap, char *);
  2012.   fprintf (stderr, "%s: ", programname);
  2013.   vfprintf (stderr, format, ap);
  2014.   va_end (ap);
  2015.  
  2016.   fprintf (stderr, "\n");
  2017. }
  2018.  
  2019. #else /* not HAVE_VPRINTF */
  2020.  
  2021. fatal (msg, arg1, arg2)
  2022.      char *msg, *arg1, *arg2;
  2023. {
  2024.   error (msg, arg1, arg2);
  2025.   delete_temp_files (0);
  2026.   exit (1);
  2027. }
  2028.  
  2029. error (msg, arg1, arg2)
  2030.      char *msg, *arg1, *arg2;
  2031. {
  2032.   fprintf (stderr, "%s: ", programname);
  2033.   fprintf (stderr, msg, arg1, arg2);
  2034.   fprintf (stderr, "\n");
  2035. }
  2036.  
  2037. #endif /* not HAVE_VPRINTF */
  2038.  
  2039.  
  2040. void
  2041. validate_all_switches ()
  2042. {
  2043.   struct compiler *comp;
  2044.   register char *p;
  2045.   register char c;
  2046.  
  2047.   for (comp = compilers; comp->spec; comp++)
  2048.     {
  2049.       p = comp->spec;
  2050.       while (c = *p++)
  2051.     if (c == '%' && *p == '{')
  2052.       /* We have a switch spec.  */
  2053.       validate_switches (p + 1);
  2054.     }
  2055.  
  2056.   p = link_spec;
  2057.   while (c = *p++)
  2058.     if (c == '%' && *p == '{')
  2059.       /* We have a switch spec.  */
  2060.       validate_switches (p + 1);
  2061.  
  2062.   /* Now notice switches mentioned in the machine-specific specs.  */
  2063.  
  2064. #ifdef ASM_SPEC
  2065.   p = ASM_SPEC;
  2066.   while (c = *p++)
  2067.     if (c == '%' && *p == '{')
  2068.       /* We have a switch spec.  */
  2069.       validate_switches (p + 1);
  2070. #endif
  2071.  
  2072. #ifdef CPP_SPEC
  2073.   p = CPP_SPEC;
  2074.   while (c = *p++)
  2075.     if (c == '%' && *p == '{')
  2076.       /* We have a switch spec.  */
  2077.       validate_switches (p + 1);
  2078. #endif
  2079.  
  2080. #ifdef SIGNED_CHAR_SPEC
  2081.   p = SIGNED_CHAR_SPEC;
  2082.   while (c = *p++)
  2083.     if (c == '%' && *p == '{')
  2084.       /* We have a switch spec.  */
  2085.       validate_switches (p + 1);
  2086. #endif
  2087.  
  2088. #ifdef CC1_SPEC
  2089.   p = CC1_SPEC;
  2090.   while (c = *p++)
  2091.     if (c == '%' && *p == '{')
  2092.       /* We have a switch spec.  */
  2093.       validate_switches (p + 1);
  2094. #endif
  2095.  
  2096. #ifdef LINK_SPEC
  2097.   p = LINK_SPEC;
  2098.   while (c = *p++)
  2099.     if (c == '%' && *p == '{')
  2100.       /* We have a switch spec.  */
  2101.       validate_switches (p + 1);
  2102. #endif
  2103.  
  2104. #ifdef LIB_SPEC
  2105.   p = LIB_SPEC;
  2106.   while (c = *p++)
  2107.     if (c == '%' && *p == '{')
  2108.       /* We have a switch spec.  */
  2109.       validate_switches (p + 1);
  2110. #endif
  2111.  
  2112. #ifdef STARTFILE_SPEC
  2113.   p = STARTFILE_SPEC;
  2114.   while (c = *p++)
  2115.     if (c == '%' && *p == '{')
  2116.       /* We have a switch spec.  */
  2117.       validate_switches (p + 1);
  2118. #endif
  2119. }
  2120.  
  2121. /* Look at the switch-name that comes after START
  2122.    and mark as valid all supplied switches that match it.  */
  2123.  
  2124. void
  2125. validate_switches (start)
  2126.      char *start;
  2127. {
  2128.   register char *p = start;
  2129.   char *filter;
  2130.   register int i;
  2131.  
  2132.   if (*p == '|')
  2133.     ++p;
  2134.  
  2135.   if (*p == '!')
  2136.     ++p;
  2137.  
  2138.   filter = p;
  2139.   while (*p != ':' && *p != '}') p++;
  2140.  
  2141.   if (p[-1] == '*')
  2142.     {
  2143.       /* Mark all matching switches as valid.  */
  2144.       --p;
  2145.       for (i = 0; i < n_switches; i++)
  2146.     if (!strncmp (switches[i].part1, filter, p - filter))
  2147.       switches[i].valid = 1;
  2148.     }
  2149.   else
  2150.     {
  2151.       /* Mark an exact matching switch as valid.  */
  2152.       for (i = 0; i < n_switches; i++)
  2153.     {
  2154.       if (!strncmp (switches[i].part1, filter, p - filter)
  2155.           && switches[i].part1[p - filter] == 0)
  2156.         switches[i].valid = 1;
  2157.     }
  2158.     }
  2159. }
  2160.